package com.zust.zst.controller.chooselesson;

import com.zust.zst.aop.HttpAuthChecker;
import com.zust.zst.chooselesson.service.CourseInfoManagementService;
import com.zust.zst.common.dto.chooselesson.AddCourseDTO;
import com.zust.zst.common.dto.chooselesson.GetCourseByTermAndCollegeDTO;
import com.zust.zst.common.dto.chooselesson.SearchCourseDTO;
import com.zust.zst.common.vo.ResultVO;
import com.zust.zst.common.vo.chooselesson.CollegeVO;
import com.zust.zst.common.vo.chooselesson.CourseInfoVO;
import com.zust.zst.common.vo.chooselesson.TermVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.zust.zst.common.enums.OperationStatus.*;

/**
 * 选课管理：课程信息管理接口
 *
 * @author Gavin Zhang
 * @date 2022/11/22 20:16
 */

@RestController
@Api(tags = "选课管理：课程信息管理接口")
@Slf4j
@RequestMapping("/chooselesson/courseInfoManagement")
public class CourseInfoManagementController {

    @Resource(name = "courseInfoManagementServiceImpl")
    private CourseInfoManagementService courseInfoManagementService;

    /**
     * 获取学期
     *
     * @return 获取到的学期
     */
    @ApiOperation(value = "获取全部学期")
    @GetMapping("/getAllTerm")
    @HttpAuthChecker
    public ResultVO<TermVO> getAllTerm() {
        TermVO termVO = new TermVO();
        List<String> terms = courseInfoManagementService.getAllTerm();
        if (terms != null) {
            termVO.setTerms(terms);
            log.info("获取了全部学期{}", termVO);
            return ResultVO.success("全部学期", termVO);
        }
        log.warn("获取全部学期失败");
        return ResultVO.error("获取全部学期信息失败");
    }

    /**
     * 获取全部学院
     *
     * @return 获取到的全部学院
     */
    @ApiOperation(value = "获取全部学院")
    @GetMapping("/getAllColleges")
    @HttpAuthChecker
    public ResultVO<CollegeVO> getAllColleges() {
        CollegeVO collegeVO = new CollegeVO();
        List<String> colleges = courseInfoManagementService.getAllColleges();
        if (colleges != null) {
            collegeVO.setColleges(colleges);
            log.info("获取了全部学院{}", collegeVO);
            return ResultVO.success("全部学院", collegeVO);
        }
        log.warn("获取全部学院失败");
        return ResultVO.error("获取全部学院信息失败");
    }

    /**
     * 插入课程信息
     */
    @ApiOperation(value = "插入课程信息")
    @PostMapping("/addCourse")
    @HttpAuthChecker
    public ResultVO<String> addCourse(@RequestBody AddCourseDTO addCourseDTO) {
        if (courseInfoManagementService.addCourse(addCourseDTO) == INSERT_FAILED.getCode()) {
            log.warn("插入失败：{}", addCourseDTO);
            return ResultVO.error(INSERT_FAILED.getDescription());
        }
        log.info("插入成功：{}", addCourseDTO);
        return ResultVO.success("插入成功", null);
    }

    /**
     * 通过开设学期和开设学院获取课程信息
     *
     * @return 获取到的对应开设学期和开设学院获取课程信息
     */
    @ApiOperation(value = "通过开设学期和开设学院获取课程信息")
    @PostMapping("/getCourseByTermAndCollege")
    @HttpAuthChecker
    public ResultVO<List<CourseInfoVO>> getCourseByTermAndCollege(@RequestBody GetCourseByTermAndCollegeDTO dto) {
        List<CourseInfoVO> courseInfoVO = courseInfoManagementService.getCourseByTermAndCollege(dto);
        if (courseInfoVO != null) {
            log.info("课程信息：{}", courseInfoVO);
            return ResultVO.success("课程信息", courseInfoVO);
        }
        log.warn("暂无相应课程信息！");
        return ResultVO.error("暂无相应课程信息!");
    }

    /**
     * 通过开设学期和开设学院获取课程信息条数
     *
     * @return 获取到的对应开设学期和开设学院获取课程信息条数
     */
    @ApiOperation(value = "通过开设学期和开设学院获取课程信息的总数")
    @GetMapping("/getTotalLineByTermAndCollege/{term}/{college}")
    @HttpAuthChecker
    public ResultVO<Integer> getTotalLineByTermAndCollege(@PathVariable String term, @PathVariable String college) {
        int count = courseInfoManagementService.getTotalLineByTermAndCollege(term, college);
        log.info("学期：{}，学院：{}，对应开设课程信息条数：{}", term, college, count);
        return ResultVO.success("对应课程信息条数", count);
    }

    /**
     * 通过课程名获取相应课程信息
     *
     * @return 获取到的对应开设学期和开设学院获取课程信息
     */
    @ApiOperation(value = "通过课程名获取相应课程信息")
    @PostMapping("/getCoursesBySearchingName")
    @HttpAuthChecker
    public ResultVO<List<CourseInfoVO>> getCoursesBySearchingName(@RequestBody SearchCourseDTO searchCourseDTO) {
        List<CourseInfoVO> courseInfoVO = courseInfoManagementService.getCoursesBySearchingName(searchCourseDTO);
        if (courseInfoVO != null) {
            log.info("课程信息：{}", courseInfoVO);
            return ResultVO.success("课程信息", courseInfoVO);
        }
        log.warn("暂无相应课程信息！");
        return ResultVO.error("暂无相应课程信息!");
    }

    /**
     * 通过开设学期和开设学院获取课程信息条数
     *
     * @return 获取到的对应开设学期和开设学院获取课程信息条数
     */
    @ApiOperation(value = "通过开设学期和开设学院获取课程信息的总数")
    @GetMapping("/getTotalLineBySearchingCourse/{term}/{college}/{course}")
    @HttpAuthChecker
    public ResultVO<Integer> getTotalLineBySearchingCourse(@PathVariable String term, @PathVariable String college, @PathVariable String course) {
        int count = courseInfoManagementService.getTotalLineBySearchingCourse(term, college, course);
        log.info("学期：{}，学院：{}，课程名{}，对应开设课程信息条数：{}", term, college, course, count);
        return ResultVO.success("对应课程信息条数", count);
    }


    /**
     * 更新课程信息
     *
     * @return 更新操作状态
     */
    @ApiOperation(value = "更新课程信息")
    @PostMapping("/updateCourse")
    @HttpAuthChecker
    public ResultVO<String> updateCourse(@RequestBody CourseInfoVO courseInfoVO) {
        if (courseInfoManagementService.updateCourse(courseInfoVO) == UPDATE_FAILED.getCode()) {
            log.warn("更新失败：{}", courseInfoVO);
            return ResultVO.error("更新失败");
        }
        log.info("更新成功：{}", courseInfoVO);
        return ResultVO.success("更新成功", null);
    }

    /**
     * 删除课程
     *
     * @return 删除课程操作状态
     */
    @ApiOperation(value = "删除课程")
    @GetMapping("/deleteCourseById/{courseId}")
    @HttpAuthChecker
    public ResultVO<String> deleteCourseById(@PathVariable Integer courseId) {
        if (courseInfoManagementService.deleteCourseById(courseId) == DELETE_FAILED.getCode()) {
            log.warn("删除失败，id：{}", courseId);
            return ResultVO.error("删除失败");
        }
        log.info("删除成功：{}", courseId);
        return ResultVO.success("删除成功", null);
    }

}